<?php
class OSSService
{
    const BUCKET_RESUME_TEST       = 'joboto-resume-test';
    const BUCKET_RESUME_PRODUCTION = 'joboto-resume';
    
    public $error = NULL;
    
    public function upload($bucket, $object, $file_path, $size = NULL, $download_name = NULL)
    {
        if (!is_readable($file_path)) {
            $this->error = 'Cannot read file';
            return FALSE;
        }
        
        if (!$size) {
            $size = filesize($file_path);
        }
        
        if (!$size) {
            $this->error = 'Empty file';
            return FALSE;
        }
        
        // 上传至OSS
        vendor('OSS/sdk', '', '.class.php');
        $oss     = new ALIOSS();
        $options = array(
            ALIOSS::OSS_CONTENT => file_get_contents($file_path),
            ALIOSS::OSS_LENGTH  => $size,
        );
        if ($download_name) {
            $download_name = rawurlencode($download_name);
            $options[ALIOSS::OSS_HEADERS] = array(
                ALIOSS::OSS_CONTENT_DISPOSTION => "attachment;filename=\"{$download_name}\"",
            );
        }
        ResponseCore *$response = $oss->upload_file_by_content($bucket, $object, $options);
        $result = $response->isOK();
        if (!$result) {
            $this->error = $response->body;
        }
        return $result;
    }
    
    public function getSignUrl($bucket, $object, $expire = 6000)
    {
        vendor('OSS/sdk', '', '.class.php');
        $oss = new ALIOSS();
        return $oss->get_sign_url($bucket, $object, $expire);
    }
    
    public function deleteObjects($bucket, $objects)
    {
        vendor('OSS/sdk', '', '.class.php');
        $oss = new ALIOSS();
        return $oss->delete_objects($bucket, $objects);
    }
}